﻿using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using EmptyWallGallery.Core;

namespace EmptyWallGallery.FrontEnd.inside.Manage
{
    public partial class EditUser : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            var roleArray = Roles.GetAllRoles();
            cboList.DataSource = roleArray;
            cboList.DataBind();

            var strR = Roles.GetRolesForUser(Request.QueryString["u"]);
            foreach (var t in strR)
            {
                cboList.SelectedValue = t;
            }

            var user = Membership.GetUser(Request.QueryString["u"]);
            if (user == null) return;

            if (user.IsApproved)
            {
                BntLock.Visible = false;
            }

            txtEmail.Text = user.Email;
            txtUserName.Text = user.UserName;

            if (!string.IsNullOrEmpty(Request.QueryString["Msg"]))
            {
                lblMessage.Text = "* Đăng ký thành công.<br />* Mật khẩu thành viên: <b>" + Request.QueryString["Pwd"] + "</b>";
            }

            Page.Title = "Thay đổi thành viên " + user.UserName + " - " + Configs.WebTitle;

        }

        protected void BtnUpdateClick(object sender, EventArgs e)
        {
            var user = Membership.GetUser(Request.QueryString["u"]);
            if (user != null)
            {
                user.Email = txtEmail.Text;
                Membership.UpdateUser(user);
            }

            UpdateRolesFromList(Request.QueryString["u"], cboList);

            lblMessage.Text = "* Thông tin thành viên đã được cập nhật.";

        }

        public void UpdateRolesFromList(string username, CheckBoxList checkBoxList)
        {
            foreach (ListItem roleListItem in checkBoxList.Items)
            {
                var roleName = roleListItem.Value;
                var userName = username;
                var enableRole = roleListItem.Selected;

                if (enableRole & (Roles.IsUserInRole(userName, roleName) == false))
                {
                    Roles.AddUserToRole(userName, roleName);
                }
                else if ((enableRole == false) & Roles.IsUserInRole(userName, roleName))
                {
                    Roles.RemoveUserFromRole(userName, roleName);
                }
            }
        }

        protected void BtnResetPasswordClick(object sender, EventArgs e)
        {
            var user = Membership.GetUser(Request.QueryString["u"]);
            if (user == null) return;

            var strPasswordTemp = user.ResetPassword();

            Core.Utilities.Common.Instance.SendEmailPassword(Configs.WebName + " - Mat khau quan tri", user.UserName,
                                                             user.Email, strPasswordTemp);

            lblMessage.Text = "* Mật khẩu đã được thay đổi. Password: <b>" + strPasswordTemp + "</b>";
        }

        protected void BtnLockClick(object sender, EventArgs e)
        {
            try
            {
                var user = Membership.GetUser(Request.QueryString["u"]);
                if (user == null) return;

                user.IsApproved = !user.IsApproved;
                Membership.UpdateUser(user);

                Page.RegisterStartupScript("Notify",
                                           "<script>alert('Thành viên " + user.UserName +
                                           " đã được mở khóa.');location.href = 'default.aspx';</script>");

                //using (var sReader = new System.IO.StreamReader(Core.Configs.FolderSaveData + @"\other\LostPassword_Admin.htm"))
                //{
                //    var strTemp = sReader.ReadToEnd();
                //    strTemp = strTemp.Replace("{0}", user.UserName);
                //    strTemp = strTemp.Replace("{1}", strPasswordTemp);
                //    strTemp = strTemp.Replace("{2}", Core.Configs.HomeUrl + "/Account/ChangePassword.aspx");

                //    //var mailer = new Core..Services.Srv.ShopSrv();
                //    //mailer.SendMail(user.Email, "Mật khẩu quản trị", strTemp, "");
                //}
            }
            catch (Exception)
            {
                Response.Write("Error !!");
                Response.Redirect("default.aspx");
                //Response.End();
            }
        }
    }
}